package controller;

import dao.AttendanceDAO;
import dao.CreditDAO;
import model.AttendanceRecord;
import model.CreditLog;
import dao.AttendanceDAO.CheckInResult;
import dao.AttendanceDAO.CheckOutResult;
import dao.CreditDAO.CreditStatistics;
import dao.CreditDAO.SystemCreditOverview;

import java.time.LocalDate;
import java.util.List;

/**
 * 考勤控制器
 */
public class AttendanceController {
    private AttendanceDAO attendanceDAO;
    private CreditDAO creditDAO;

    public AttendanceController() {
        this.attendanceDAO = new AttendanceDAO();
        this.creditDAO = new CreditDAO();
    }

    /**
     * 签到
     */
    public CheckInResult checkIn(int reservationId, int userId) {
        try {
            return attendanceDAO.checkIn(reservationId, userId);
        } catch (Exception e) {
            e.printStackTrace();
            return new CheckInResult(false, -1, "签到失败: " + e.getMessage());
        }
    }

    /**
     * 签退
     */
    public CheckOutResult checkOut(int reservationId, int userId) {
        try {
            return attendanceDAO.checkOut(reservationId, userId);
        } catch (Exception e) {
            e.printStackTrace();
            return new CheckOutResult(false, -1, "签退失败: " + e.getMessage());
        }
    }

    /**
     * 获取用户今天的活跃考勤记录（进行中的）
     */
    public List<AttendanceRecord> getUserTodayActiveAttendance(int userId) {
        try {
            return attendanceDAO.getUserTodayActiveAttendance(userId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取今日考勤记录失败: " + e.getMessage());
        }
    }

    /**
     * 获取用户今天的所有预约
     */
    public List<AttendanceRecord> getUserTodayAllReservations(int userId) {
        try {
            return attendanceDAO.getUserTodayAllReservations(userId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取今日预约记录失败: " + e.getMessage());
        }
    }

    /**
     * 获取用户的考勤记录
     */
    public List<AttendanceRecord> getUserAttendanceRecords(int userId, LocalDate startDate, LocalDate endDate) {
        try {
            return attendanceDAO.getUserAttendanceRecords(userId, startDate, endDate);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取考勤记录失败: " + e.getMessage());
        }
    }

    /**
     * 获取用户指定日期范围内的所有预约（包括没有考勤记录的预约）
     */
    public List<AttendanceRecord> getUserAllReservations(int userId, LocalDate startDate, LocalDate endDate) {
        try {
            return attendanceDAO.getUserAllReservations(userId, startDate, endDate);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取预约记录失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有考勤记录（管理员使用）
     */
    public List<AttendanceRecord> getAllAttendanceRecords(LocalDate startDate, LocalDate endDate,
            Integer attendanceStatus) {
        try {
            return attendanceDAO.getAllAttendanceRecords(startDate, endDate, attendanceStatus);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取考勤记录失败: " + e.getMessage());
        }
    }

    /**
     * 处理逾期预约
     */
    public boolean processOverdueReservations() {
        try {
            attendanceDAO.processOverdueReservations();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 更新用户连续使用记录
     */
    public boolean updateUserStreaks() {
        try {
            attendanceDAO.updateUserStreaks();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取用户的信誉分记录
     */
    public List<CreditLog> getUserCreditLogs(int userId, LocalDate startDate, LocalDate endDate) {
        try {
            return creditDAO.getUserCreditLogs(userId, startDate, endDate);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取信誉分记录失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有信誉分记录（管理员使用）
     */
    public List<CreditLog> getAllCreditLogs(LocalDate startDate, LocalDate endDate, Integer logType) {
        try {
            return creditDAO.getAllCreditLogs(startDate, endDate, logType);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取信誉分记录失败: " + e.getMessage());
        }
    }

    /**
     * 管理员调整用户信誉分
     */
    public boolean adjustUserCreditScore(int userId, int scoreChange, String reason) {
        try {
            return creditDAO.adjustUserCreditScore(userId, scoreChange, reason);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("调整信誉分失败: " + e.getMessage());
        }
    }

    /**
     * 获取用户信誉分统计信息
     */
    public CreditStatistics getUserCreditStatistics(int userId) {
        try {
            return creditDAO.getCreditStatistics(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return new CreditStatistics(0, 0, 0, 100.0);
        }
    }

    /**
     * 获取系统信誉分概览
     */
    public SystemCreditOverview getSystemCreditOverview() {
        try {
            return creditDAO.getSystemCreditOverview();
        } catch (Exception e) {
            e.printStackTrace();
            return new SystemCreditOverview(0, 0, 0, 0, 0, 0);
        }
    }

    /**
     * 根据预约ID获取考勤记录
     */
    public AttendanceRecord getAttendanceByReservationId(int reservationId) {
        try {
            return attendanceDAO.getAttendanceByReservationId(reservationId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 创建考勤记录
     */
    public boolean createAttendanceRecord(AttendanceRecord record) {
        try {
            return attendanceDAO.insertAttendanceRecord(record);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("创建考勤记录失败: " + e.getMessage());
        }
    }
}
